﻿using Nasty.Core.Session;
using Nasty.Core;
using Nasty.Core.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nasty.Core.Attributes;

namespace Nasty.Core.Entity.System
{
    [SugarTable("SysSerialNumberConfig", TableDescription = "流水号配置")]
    public class SerialNumberConfig : StandardEntity<SerialNumberConfig>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [NastyColumn(ColumnName = "Name", ColumnDescription = "名称", Required = true)]
        public string? Name { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        [NastyColumn(ColumnName = "Code", ColumnDescription = "编码", IsRepeat = true)]
        public string? Code { get; set; }


        /// <summary>
        /// 前缀
        /// </summary>
        [SugarColumn(ColumnName = "Prefix", ColumnDescription = "前缀")]
        public string? Prefix { get; set; }


        /// <summary>
        /// 时间格式
        /// </summary>
        [SugarColumn(ColumnName = "TimeFormat", ColumnDescription = "时间格式")]
        public string? TimeFormat { get; set; }

        /// <summary>
        /// 自增长度
        /// </summary>
        [SugarColumn(ColumnName = "AutoIncrementLength", ColumnDescription = "自增长度")]
        public int AutoIncrementLength { get; set; }

        /// <summary>
        /// 当前自增
        /// </summary>
        [SugarColumn(ColumnName = "CurrentIncrement", ColumnDescription = "当前自增")]
        public int CurrentIncrement { get; set; }


        public override void OnPreAdd()
        {
            base.OnPreAdd();

            var db = AppSession.CurrentDb.Value;

            //var isAny = db.Queryable<SerialNumberConfig>().Where((t) => t.Code == Code).Any();
            //if (isAny) throw new Exception("编码重复");
        }


        public override void OnPreDelete()
        {
            base.OnPreDelete();
            var db = AppSession.CurrentDb.Value;
        }
    }
}
